package Jets.ui
{
   import flash.display.DisplayObjectContainer;
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.geom.Rectangle;
   import Jets.display.JContainer;
  
   
   public class JList extends JContainer
   {
	   static var selss:String = "selEffect";
	   
	   protected var vslide:JVScorllBar;
	   protected var rowheight:Number = 20;
	   protected var blocks:Array;
	   
	   public var ismenu:Boolean = false;
	   
	   private var arr:Array;
	   private var _scrollMode:String = "auto";
	   private var _underLayer:Boolean = false;
	   private var _selEffect:Sprite;
	   private var _overEffect:Sprite;
	   
      public function JList(parent:DisplayObjectContainer=null, cx:Number=0, cy:Number=0, data:*=null, render:Class=null) 
	  {
         this.blocks = [];
         this.RenderCell = (render) || (JPushButton);
         super(parent,cx,cy);
         this.data = data;
      }
      
      public function get underLayerEffect() : Boolean {
         return this._underLayer;
      }
      
      public function set underLayerEffect(param1:Boolean) : void {
         if(this._underLayer != param1)
         {
            this._underLayer = param1;
            invalidate();
         }
      }
      
      public function get overEffect() : Sprite {
         return this._overEffect;
      }
      
      public function set overEffect(param1:Sprite) : void {
         if(param1)
         {
            this._overEffect = param1;
            this._overEffect.mouseChildren = false;
            this._overEffect.mouseEnabled = false;
         }
      }
      
      public function get selEffect() : Sprite {
         return this._selEffect;
      }
      
      public function set selEffect(param1:Sprite) : void {
         if(param1)
         {
            this._selEffect = param1;
            this._selEffect.mouseChildren = false;
            this._selEffect.mouseEnabled = false;
         }
      }
      
      public function get scrollMode() : String {
         return this._scrollMode;
      }
      
      public function set scrollMode(param1:String) : void {
         this._scrollMode = param1;
         invalidate();
      }
      
      public function get RenderCell() : Class 
	  {
         return this._RenderCell;
      }
      public function set RenderCell(render:Class) : void 
	  {
         if(this._RenderCell != render)
         {
            var i:int = 0;
            while(i < this.blocks.length)
            {
               this.recycle(i);
               i++;
            }
            this._RenderCell = render;
            invalidate();
         }
      }
      
      protected function getCell() : * 
	  {
         return new this.RenderCell();
      }
      
      public function getRenderCell(index:int) : JContainer 
	  {
         var cell:JContainer = this.getCell();
		 cell.name = "" + index;
		 cell.addEventListener(MouseEvent.CLICK,this.onsel);
		 cell.addEventListener(MouseEvent.ROLL_OVER,this.onover);
		 cell.addEventListener(MouseEvent.ROLL_OUT,this.onout);
		 cell.height = this.rowheight;
         return cell as JContainer;
      }
      
      protected function onout(event:MouseEvent) : void 
	  {
         
      }
      
      protected function onover(param1:MouseEvent) : void 
	  {
         
      }
      
      private function recycle(index:int) : void 
	  {
         if((this.blocks[index]) && (this.container.contains(this.blocks[index])))
         {
            this.container.removeChild(this.blocks[index]);
            (this.blocks[index] as JContainer).removeEventListener(MouseEvent.CLICK,this.onsel);
            //ObjectPool.recycle(this.blocks[index]);
            delete this.blocks[[index]];
         }
      }
      
      public var top:Number;
      
      private var _RenderCell:Class;
      
      private var _selindex:int = -1;
      
      public var usefilter:Array;
      
      public var container:Sprite;
      
      var dragfunc:Function;
      
      var movefunc:Function;
      
      public function setdrag(param1:Function, param2:Function=null) : void 
	  {
         this.movefunc = param2;
         this.dragfunc = param1;
      }
      
      public function get selindex() : int 
	  {
         return this._selindex;
      }
      
      public function set selindex(index:int) : void 
	  {
         if((this.arr) && index >= this.arr.length)
         {
			 index = this.arr.length-1;
         }
         if(this._selindex != index)
         {
            this._selindex = index;
            this.dispatchEvent(new Event(Event.CHANGE));
         }
         invalidate();
      }
      
      public function get seldata() : Object 
	  {
         if(this.arr)
         {
            if(this._selindex >= 0)
            {
               return this.arr[this._selindex];
            }
         }
         return null;
      }
      
      override public function set data(value:Object) : void 
	  {
         if(value == null)
         {
			 value = new Array();
         }

         this.vslide.value = 100;
         this.arr = value as Array;
         if(value == null || value.length == 0)
         {
            this.selindex = -1;
         }
         else
         {
            if(this._selindex >= value.length)
            {
               this.selindex = 0;
               this.dispatchEvent(new Event(Event.CHANGE));
            }
         }
         invalidate();
      }
      
      override public function get data() : Object 
	  {
         return this.arr;
      }
      
      public function setItem(index:int, value:Object) : void 
	  {
         this.arr[index] = value;
         invalidate();
      }
      
      public function addItem(value:Object) : void 
	  {
         this.arr.push(value);
         invalidate();
      }
      
      public function removeItem(index:uint) : void 
	  {
         if(index < this.arr.length)
         {
            this.arr.splice(index,1);
         }
         this.invalidate();
      }
      
      public function getItem(index:int) : * 
	  {
         return this.arr[index];
      }
      
      override protected function addChildren() : void 
	  {
         super.addChildren();
         this.setSize(120,120);
         this.container = new Sprite();
         this.addChild(this.container);
         this.vslide = new JVScorllBar(this,this.width - JScrollBar.DefaultWidth,0,this.scroll);
         this.vslide.value = 100;
         this.vslide.backClick = true;
         invalidate();
         this.container.addEventListener(MouseEvent.MOUSE_WHEEL,this.posvs);
      }
      
      protected function posvs(event:MouseEvent) : void 
	  {
         if(this.vslide.visible)
         {
            this.vslide.value = this.vslide.value + event.delta * 2;
            invalidate();
         }
      }
      
      public function onsel(event:MouseEvent) : void 
	  {
         if(this.ismenu)
         {
            if(this.parent)
            {
               parent.removeChild(this);
            }
         }
         this.dosel(int(event.currentTarget.name));
         this.dispatchEvent(new Event(Event.SELECT));
      }
      
      public function dosel(index:int) : void 
	  {
         if(this._selindex != index)
         {
            this.selindex = index;
         }
         this.invalidate();
      }
      
      private function scroll(event:Event) : void 
	  {
         this.invalidate();
      }
      
      private var totalheight:int = 0;
      
      private var topline:int = 0;
      
      private var lastline:int = 0;
      
      public var lockSel:Boolean = false;
      
      public function scrollto(param1:int) : void {
         if(param1 < 0)
         {
            param1 = 0;
         }
         else
         {
            if(param1 >= this.arr.length)
            {
               param1 = this.arr.length-1;
            }
         }
         this.vslide.value = 100 - 100 * param1 / (this.arr.length - this.height / this.rowheight);
         invalidate();
      }
      
      override public function draw() : void {
         var _loc1_:* = 0;
         var _loc2_:* = 0;
         var _loc3_:* = 0;
         var _loc4_:* = 0;
         super.draw();
         if(this._RenderCell == null)
         {
            return;
         }
         this.vslide.x = this.width - this.vslide.width;
         this.vslide.height = this.height;
         this.vslide.visible = false;
         this.vslide._handle.visible = false;
         if(this.ismenu)
         {
            this.height = this.rowheight * this.data.length;
            _loc1_ = 0;
            while(_loc1_ < this.data.length)
            {
               if(this.blocks[_loc1_] == null)
               {
                  this.blocks[_loc1_] = this.getRenderCell(_loc1_);
                  this.blocks[_loc1_].y = this.rowheight * _loc1_;
               }
               (this.blocks[_loc1_] as JContainer).data = this.arr[_loc1_];
               this.blocks[_loc1_].setSize(this.width,this.rowheight);
               this.container.addChild(this.blocks[_loc1_]);
               _loc1_++;
            }
            _loc1_ = this.data.length;
            while(_loc1_ < this.blocks.length)
            {
               if((this.blocks[_loc1_]) && (this.container.contains(this.blocks[_loc1_])))
               {
                  this.container.removeChild(this.blocks[_loc1_]);
               }
               _loc1_++;
            }
            return;
         }
         if(this.arr)
         {
            this.totalheight = this.arr.length * this.rowheight - this.height;
            if(this.totalheight <= 0)
            {
               this.totalheight = 0;
               if(this.scrollMode == "auto")
               {
                  this.vslide.visible = false;
               }
               else
               {
                  this.vslide.visible = true;
                  this.vslide._handle.visible = false;
               }
            }
            else
            {
               if(this.arr.length > 0)
               {
                  this.vslide.visible = true;
                  this.vslide._handle.visible = true;
                  _loc4_ = (this.height - this.vslide.arrowheight * 2) * (this.arr.length * this.rowheight - this.totalheight) / this.arr.length * this.rowheight;
                  this.vslide.barheight = Math.max(_loc4_,20);
               }
            }
            this.topline = Math.round((100 - this.vslide.value) * this.totalheight / this.rowheight / 100);
            if(this.topline < 0)
            {
               this.topline = 0;
            }
            this.lastline = Math.min(Math.round(this.topline + this.height / this.rowheight),this.arr.length);
            if((this.lockSel) && this.selindex >= 0 && (this.selindex < this.topline || this.selindex >= this.lastline))
            {
               this.vslide.value = 100 - 100 * this.selindex / (this.arr.length - this.height / this.rowheight);
               invalidate();
               return;
            }
            _loc1_ = this.topline;
            while(_loc1_ < this.lastline)
            {
               if(this.blocks[_loc1_] == null)
               {
                  this.blocks[_loc1_] = this.getRenderCell(_loc1_);
                  this.blocks[_loc1_].y = this.rowheight * _loc1_;
               }
               (this.blocks[_loc1_] as JContainer).data = this.arr[_loc1_];
               if(this.vslide.visible)
               {
                  (this.blocks[_loc1_] as JContainer).width = this.width - this.vslide.width;
               }
               else
               {
                  (this.blocks[_loc1_] as JContainer).width = this.width;
               }
               this.blocks[_loc1_].height = this.rowheight;
               this.container.addChild(this.blocks[_loc1_]);
               _loc1_++;
            }
            if((this.selEffect) && (this.selEffect.parent))
            {
               this.selEffect.parent.removeChild(this.selEffect);
            }
            if(this._selindex >= 0 && (this.blocks[this._selindex]))
            {
               (this.blocks[this._selindex] as JContainer).filters = this.usefilter;
               if(this.selEffect)
               {
                  this.selEffect.x = (this.blocks[this.selindex] as JContainer).x;
                  this.selEffect.y = (this.blocks[this.selindex] as JContainer).y;
                  if(this._underLayer)
                  {
                     this.container.addChildAt(this.selEffect,0);
                  }
                  else
                  {
                     this.container.addChild(this.selEffect);
                  }
               }
            }
            _loc2_ = this.blocks.length;
            _loc3_ = 0;
            while(_loc3_ < _loc2_)
            {
               if(_loc3_ < this.topline || _loc3_ >= this.lastline)
               {
                  this.recycle(_loc3_);
               }
               _loc3_++;
            }
            this.container.scrollRect = new Rectangle(0,this.rowheight * this.topline,this.width,this.height);
            this.addChild(this.vslide);
         }
      }
   }
}
